/*
	Диалог "входящие открытки"
*/	
package dialogs {
	import Classes.pi_events;
	
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class pi_cardsBox extends pi_listWindow {
		private const CARD_SIZE: Point = new Point(200, 140);
		private const CARDS_PERLINE: int = 2;
		private var _AsInBox: Boolean;
		
 
		public function pi_cardsBox(parent: uif_component, cardList: Array, AsInBox: Boolean=true) {
			super(parent, 0, 0, 480, 400, 'cards');
			toCenter();
			_AsInBox = AsInBox;
			title.text = AsInBox?pi_data.INBOX:pi_data.OUTBOX;
			createList(cardList);
			description = AsInBox?pi_data.INBOXDESCRIPTION:pi_data.OUTBOXDESCRIPTION;
			app.addEventListener(pi_events.CARDREMOVED, doCardRemoved);
			_area.addEvent(Event.CHANGE, doChange); 
		}
		
		override protected function get areaHeight(): int {
			return 280;
		}
		
		protected function doChange(e: Event): void {
			app.dispatchEvent(new ObjectEvent(pi_events.CARDSELECT, 
								_area.selectItem.control.cardData));
			doClose(e);
		}
		
		protected function doCardRemoved(e: ObjectEvent): void {
			var list: Array = new Array()
			for (var i:int=0; i<_area.items.length; i++) 
				if (_area.items[i].object.send_id != e.object.send_id) {
					list.push(_area.items[i].object);
				} else {
					var ctrl: uif_component = _area.items[i].control;
					ctrl.fx('scaleCenter', ctrl.scale, ctrl.scale / 10).start(function(): void {
						createList(list);
					});
				}
		}

		override protected function canClose(): int {
			return 1;
		}
		
		private function createList(cardList: Array): void {
			_area.assignList(cardList, CARD_SIZE.x, function(_scroll: uif_component, inner: Rectangle, index: int): pi_cardItem {
				var card: pi_cardItem = new pi_cardItem(_scroll, cardList[index], inner.x, inner.y, CARD_SIZE.x, CARD_SIZE.y, _AsInBox);
				return card;   
			}, CARDS_PERLINE, false);
			
		}
	}
}